package com.fr.cell.core;

import com.fr.base.BaseUtils;
import com.fr.base.ColumnRow;
import com.fr.base.Inter;
import com.fr.base.core.list.IntList;
import com.fr.cell.Grid;
import com.fr.cell.GridRow;
import com.fr.cell.GridSelection;
import com.fr.cell.ReportPane;
import com.fr.report.CellElement;
import com.fr.report.Report;
import com.fr.report.TemplateReport;
import com.fr.report.core.DynamicValueList;
import com.fr.report.core.ReportHelper;
import com.fr.util.Utils;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import javax.swing.JPopupMenu;
import javax.swing.JScrollBar;
import javax.swing.JToolTip;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputAdapter;

public class GridRowMouseHandler extends MouseInputAdapter
{
  private GridRow gridRow;
  private int dragType = GridUtils.DRAG_NONE;
  private boolean isMultiRowsSelectDragPermited = false;
  private int startMultiSelectIndex = 0;
  private int endMultiSelectIndex = 0;
  private boolean isDragPermited = false;
  private int dragRowIndex = 0;
  private JToolTip rowTip = null;
  private JWindow tipWindow = null;

  public GridRowMouseHandler(GridRow paramGridRow)
  {
    this.gridRow = paramGridRow;
  }

  public void setStartMultiSelectIndex(int paramInt)
  {
    this.startMultiSelectIndex = paramInt;
  }

  public void setEndMultiSelectIndex(int paramInt)
  {
    this.endMultiSelectIndex = paramInt;
  }

  public void mousePressed(MouseEvent paramMouseEvent)
  {
    GridSelection localGridSelection;
    Object localObject;
    if (!(this.gridRow.isEnabled()))
      return;
    ReportPane localReportPane = this.gridRow.getReportPane();
    TemplateReport localTemplateReport = localReportPane.getEditingReport();
    localReportPane.getGrid().stopEditing();
    int i = paramMouseEvent.getX();
    int j = paramMouseEvent.getY();
    boolean bool1 = paramMouseEvent.isShiftDown();
    boolean bool2 = paramMouseEvent.isControlDown();
    boolean bool3 = SwingUtilities.isRightMouseButton(paramMouseEvent);
    int k = localTemplateReport.getRowCount() - 1;
    ColumnRow localColumnRow = GridUtils.getAdjustEventColumnRow(localReportPane, i, j);
    if ((!(localReportPane.isRowEndless())) && (localColumnRow.getRow() > k + 1))
      return;
    DynamicValueList localDynamicValueList = ReportHelper.getRowHeightList(localTemplateReport);
    int l = localReportPane.getGrid().getVerticalValue();
    int i1 = localReportPane.getGrid().getVerticalExtent();
    int i2 = l + i1 + 1;
    int i3 = localReportPane.getGrid().getVerticalBeginValue();
    double d1 = 0.0D;
    double d3 = 0.0D;
    for (int i4 = i3; i4 < i2; ++i4)
    {
      double d2;
      if (i4 == 0)
        i4 = l;
      d1 += d3;
      d3 = localDynamicValueList.get(i4);
      if (d3 <= 0.0D)
        d2 = d1 + 1.0D;
      else
        d2 = d1 + d3;
      if ((isOnZeroSeparatorLine(j, d2, d3)) || (isOnNormalSeparatorLine(j, d2)))
      {
        this.dragType = GridUtils.DRAG_HEIGHT;
        this.isDragPermited = true;
        this.dragRowIndex = i4;
        showToolTip(paramMouseEvent, createToolTipString(localDynamicValueList.get(this.dragRowIndex), localDynamicValueList.getRangeValue(0, this.dragRowIndex + 1)));
        break;
      }
      if ((j > d1) && (j <= d2))
      {
        this.dragType = GridUtils.DRAG_SELECT_ROWS;
        this.isMultiRowsSelectDragPermited = true;
        this.startMultiSelectIndex = i4;
        showToolTip(paramMouseEvent, createToolTipString(1));
        break;
      }
    }
    if (bool3)
    {
      localGridSelection = localReportPane.getGridSelection();
      localGridSelection.setType(0);
      localObject = GridUtils.getGridSelectionRowIndex(localGridSelection);
      if ((localColumnRow.getRow() < ((IntList)localObject).get(0)) || (localColumnRow.getRow() > ((IntList)localObject).get(((IntList)localObject).size() - 1)))
        doSelectRow(localColumnRow.getRow());
      JPopupMenu localJPopupMenu = localReportPane.createRowPopupMenu(paramMouseEvent, Math.max(this.dragRowIndex, Math.min(this.startMultiSelectIndex, this.endMultiSelectIndex)));
      if (localJPopupMenu != null)
        GUICoreUtils.showPopupMenu(localJPopupMenu, this.gridRow, i + 1, j + 1);
    }
    else if (this.dragType == GridUtils.DRAG_SELECT_ROWS)
    {
      localGridSelection = localReportPane.getGridSelection();
      localGridSelection.setType(0);
      if (bool1)
      {
        localObject = null;
        if (bool2)
          localObject = localGridSelection.getLastCellRectangle();
        else
          localObject = localGridSelection.getFirstCellRectangle();
        doShiftSelectRow(localReportPane, i, j, (Rectangle)localObject);
      }
      else if (bool2)
      {
        doControlSelectRow(localReportPane, i, j);
      }
      else
      {
        localColumnRow = GridUtils.getEventColumnRow(localReportPane, i, j);
        doSelectRow(localColumnRow.getRow());
      }
    }
    this.gridRow.getReportPane().repaint();
  }

  private void doShiftSelectRow(ReportPane paramReportPane, double paramDouble1, double paramDouble2, Rectangle paramRectangle)
  {
    ColumnRow localColumnRow = GridUtils.getAdjustEventColumnRow(paramReportPane, paramDouble1, paramDouble2);
    int i = localColumnRow.getColumn();
    int j = localColumnRow.getRow();
    GridSelection localGridSelection = paramReportPane.getGridSelection();
    Rectangle localRectangle1 = localGridSelection.getEditRectangle();
    int k = localRectangle1.y;
    if (localColumnRow.getRow() >= localRectangle1.y)
      localColumnRow = ColumnRow.valueOf(localColumnRow.getColumn(), localColumnRow.getRow() + 1);
    else
      ++k;
    int l = GridUtils.getAdjustLastColumnRowOfReportPane(paramReportPane).getColumn();
    Rectangle localRectangle2 = new Rectangle(0, Math.min(k, localColumnRow.getRow()), l, Math.max(localRectangle1.height, Math.abs(k - localColumnRow.getRow())));
    paramRectangle.setBounds(localRectangle2.x, localRectangle2.y, localRectangle2.width, localRectangle2.height);
    paramReportPane.ensureColumnRowVisible(i, j);
  }

  private void doControlSelectRow(ReportPane paramReportPane, double paramDouble1, double paramDouble2)
  {
    TemplateReport localTemplateReport = paramReportPane.getEditingReport();
    GridSelection localGridSelection = paramReportPane.getGridSelection();
    localGridSelection.setType(0);
    int i = GridUtils.getAdjustLastColumnRowOfReportPane(paramReportPane).getColumn();
    ColumnRow localColumnRow = GridUtils.getAdjustEventColumnRow(paramReportPane, paramDouble1, paramDouble2);
    CellElement localCellElement = localTemplateReport.getCellElement(localColumnRow.getColumn(), 0);
    if (localCellElement == null)
    {
      localGridSelection.setEditBounds(0, localColumnRow.getRow(), 1, 1);
      localGridSelection.addCellRectangle(new Rectangle(0, localColumnRow.getRow(), i, 1));
    }
    else
    {
      localGridSelection.setEditBounds(localCellElement.getColumn(), localCellElement.getRow(), 1, localCellElement.getRowSpan());
      localGridSelection.addCellRectangle(new Rectangle(0, localCellElement.getRow(), i, 1));
    }
    paramReportPane.setGridSelection(localGridSelection);
    dealCellSelection(paramReportPane);
    paramReportPane.ensureColumnRowVisible(localColumnRow.getColumn(), localColumnRow.getRow());
  }

  public void mouseReleased(MouseEvent paramMouseEvent)
  {
    if (!(this.gridRow.isEnabled()))
      return;
    ReportPane localReportPane = this.gridRow.getReportPane();
    localReportPane.getGrid().stopEditing();
    this.isMultiRowsSelectDragPermited = false;
    this.isDragPermited = false;
    hideToolTip();
    if (this.dragType == GridUtils.DRAG_HEIGHT)
    {
      IntList localIntList;
      DynamicValueList localDynamicValueList;
      int i2;
      int i3;
      GridSelection localGridSelection = localReportPane.getGridSelection();
      int i = localGridSelection.getType();
      if (i == 0)
      {
        int j = 1;
        int k = GridUtils.getAdjustLastColumnRowOfReportPane(localReportPane).getColumn();
        for (int l = 0; l < localGridSelection.getCellRectangleCount(); ++l)
        {
          int i1 = localGridSelection.getCellRectangle(l).width;
          if (i1 < k)
          {
            j = 0;
            break;
          }
        }
        if (j != 0)
        {
          localIntList = GridUtils.getGridSelectionRowIndex(localGridSelection);
          localDynamicValueList = ReportHelper.getRowHeightList(localReportPane.getEditingReport());
          i2 = localDynamicValueList.get(this.dragRowIndex);
          for (i3 = 0; i3 < localIntList.size(); ++i3)
          {
            int i4;
            if (localIntList.get(i3) == this.dragRowIndex)
              for (i4 = 0; i4 < localIntList.size(); ++i4)
                localDynamicValueList.set(localIntList.get(i4), i2);
          }
        }
      }
      localReportPane.getVerticalScrollBar().setValue(localReportPane.getVerticalScrollBar().getValue());
      localReportPane.getHorizontalScrollBar().setValue(localReportPane.getHorizontalScrollBar().getValue());
    }
    this.dragType = GridUtils.DRAG_NONE;
    this.dragRowIndex = 0;
    localReportPane.fireReportDataChanged();
    localReportPane.undoRecord();
  }

  public void mouseDragged(MouseEvent paramMouseEvent)
  {
    int i1;
    int i2;
    if (!(this.gridRow.isEnabled()))
      return;
    ReportPane localReportPane = this.gridRow.getReportPane();
    TemplateReport localTemplateReport = localReportPane.getEditingReport();
    if ((this.dragType == GridUtils.DRAG_NONE) || (SwingUtilities.isRightMouseButton(paramMouseEvent)))
      return;
    localReportPane.getGrid().stopEditing();
    GridSelection localGridSelection = localReportPane.getGridSelection();
    localGridSelection.setType(0);
    int i = paramMouseEvent.getX();
    int j = paramMouseEvent.getY();
    if (j < 0)
      j = 0;
    if (this.dragType == GridUtils.DRAG_SELECT_ROWS)
    {
      if (!(this.isMultiRowsSelectDragPermited))
        return;
      int k = localTemplateReport.getRowCount() - 1;
      ColumnRow localColumnRow = GridUtils.getAdjustEventColumnRow(this.gridRow.getReportPane(), i, j);
      if ((!(localReportPane.isRowEndless())) && (localColumnRow.getRow() > k + 1))
        return;
      this.endMultiSelectIndex = localColumnRow.getRow();
      i1 = GridUtils.getAdjustLastColumnRowOfReportPane(localReportPane).getColumn();
      if (this.endMultiSelectIndex > this.startMultiSelectIndex)
      {
        i2 = this.endMultiSelectIndex - this.startMultiSelectIndex + 1;
        localGridSelection.setEditBounds(0, this.startMultiSelectIndex, 1, 1);
        localGridSelection.setOnlyCellBounds(0, this.startMultiSelectIndex, i1, i2);
      }
      else if (this.endMultiSelectIndex < this.startMultiSelectIndex)
      {
        i2 = this.startMultiSelectIndex - this.endMultiSelectIndex + 1;
        localGridSelection.setEditBounds(0, this.startMultiSelectIndex, 1, 1);
        localGridSelection.setOnlyCellBounds(0, this.endMultiSelectIndex, i1, i2);
      }
      else
      {
        localGridSelection.setEditBounds(0, this.startMultiSelectIndex, 1, 1);
        localGridSelection.setOnlyCellBounds(0, this.startMultiSelectIndex, i1, 1);
      }
      dealCellSelection(localReportPane);
      setToolTipText2(createToolTipString(Math.abs(this.startMultiSelectIndex - this.endMultiSelectIndex) + 1));
      label546: localReportPane.fireGridSelectionChanged();
    }
    else
    {
      if (!(this.isDragPermited))
        return;
      DynamicValueList localDynamicValueList = ReportHelper.getRowHeightList(localTemplateReport);
      int l = localReportPane.getGrid().getVerticalValue();
      i1 = localReportPane.getGrid().getVerticalExtent();
      i2 = l + i1 + 1;
      int i3 = localReportPane.getGrid().getVerticalBeginValue();
      int i4 = 0;
      int i5 = 0;
      int i7 = 0;
      for (int i9 = i3; i9 < i2; ++i9)
      {
        int i6;
        if (i9 == 0)
          i9 = l;
        i5 += i7;
        int i8 = localDynamicValueList.get(i9);
        if (i8 <= 0)
          i6 = i5 + 1;
        else
          i6 = i5 + i8;
        if (i9 == this.dragRowIndex)
          i4 = i5;
        if ((j >= i5) && (j < i6))
        {
          if (i9 >= this.dragRowIndex)
          {
            localDynamicValueList.set(this.dragRowIndex, j - i4);
            break;
          }
          localDynamicValueList.set(i9, j - i5);
          int i10 = this.dragRowIndex - 1;
          while (true)
          {
            if (i10 <= i9)
              break label546;
            localDynamicValueList.set(i10, 0);
            --i10;
          }
        }
      }
      setToolTipText2(createToolTipString(localDynamicValueList.get(this.dragRowIndex), localDynamicValueList.getRangeValue(0, this.dragRowIndex + 1)));
    }
    this.gridRow.getReportPane().repaint();
  }

  public void mouseMoved(MouseEvent paramMouseEvent)
  {
    if (!(this.gridRow.isEnabled()))
      return;
    ReportPane localReportPane = this.gridRow.getReportPane();
    TemplateReport localTemplateReport = localReportPane.getEditingReport();
    this.gridRow.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(BaseUtils.readImage("/com/fr/cell/images/cursor/cursor_vselect.gif"), new Point(16, 16), "ver_select"));
    double d1 = paramMouseEvent.getY();
    DynamicValueList localDynamicValueList = ReportHelper.getRowHeightList(localTemplateReport);
    int i = localReportPane.getGrid().getVerticalValue();
    int j = localReportPane.getGrid().getVerticalExtent();
    int k = i + j + 1;
    int l = localReportPane.getGrid().getVerticalBeginValue();
    double d2 = 0.0D;
    double d4 = 0.0D;
    for (int i1 = l; i1 < k; ++i1)
    {
      double d3;
      if (i1 == 0)
        i1 = i;
      d2 += d4;
      d4 = localDynamicValueList.get(i1);
      if (d4 <= 0.0D)
        d3 = d2 + 1.0D;
      else
        d3 = d2 + d4;
      if (isOnZeroSeparatorLine(d1, d3, d4))
      {
        this.gridRow.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(BaseUtils.readImage("/com/fr/cell/images/cursor/cursor_vsplit.gif"), new Point(16, 16), "ver_split"));
        return;
      }
      if (isOnNormalSeparatorLine(d1, d3))
      {
        this.gridRow.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(BaseUtils.readImage("/com/fr/cell/images/cursor/cursor_vmove.gif"), new Point(16, 16), "ver_move"));
        return;
      }
    }
  }

  private void doSelectRow(int paramInt)
  {
    ReportPane localReportPane = this.gridRow.getReportPane();
    GridSelection localGridSelection = localReportPane.getGridSelection();
    localGridSelection.setType(0);
    this.startMultiSelectIndex = (this.endMultiSelectIndex = paramInt);
    int i = GridUtils.getAdjustLastColumnRowOfReportPane(localReportPane).getColumn();
    localGridSelection.setEditBounds(0, paramInt, 1, 1);
    localGridSelection.setOnlyCellBounds(0, paramInt, i, 1);
    dealCellSelection(localReportPane);
    localReportPane.fireGridSelectionChanged();
  }

  private boolean isOnZeroSeparatorLine(double paramDouble1, double paramDouble2, double paramDouble3)
  {
    return ((paramDouble3 == 0.0D) && (paramDouble1 >= paramDouble2 + 2.0D) && (paramDouble1 <= paramDouble2 + 5.0D));
  }

  private boolean isOnNormalSeparatorLine(double paramDouble1, double paramDouble2)
  {
    return ((paramDouble1 >= paramDouble2 - 2.0D) && (paramDouble1 <= paramDouble2 + 2.0D));
  }

  private void showToolTip(MouseEvent paramMouseEvent, String paramString)
  {
    if (this.tipWindow == null)
    {
      this.tipWindow = new JWindow();
      this.rowTip = this.gridRow.createToolTip();
      this.tipWindow.getContentPane().add(this.rowTip, "Center");
    }
    this.rowTip.setTipText(paramString);
    Point localPoint = new Point(0, paramMouseEvent.getY());
    SwingUtilities.convertPointToScreen(localPoint, this.gridRow);
    Dimension localDimension = this.rowTip.getPreferredSize();
    int i = localPoint.x + this.gridRow.getSize().width + 2;
    int j = localPoint.y - localDimension.height / 2;
    this.tipWindow.setLocation(i, j);
    this.tipWindow.pack();
    this.tipWindow.setVisible(true);
  }

  private void hideToolTip()
  {
    if (this.tipWindow != null)
      this.tipWindow.setVisible(false);
  }

  private void setToolTipText2(String paramString)
  {
    if (this.rowTip == null)
      return;
    this.rowTip.setTipText(paramString);
    this.rowTip.setSize(this.rowTip.getPreferredSize());
    this.tipWindow.pack();
    this.tipWindow.repaint();
  }

  private String createToolTipString(double paramDouble1, double paramDouble2)
  {
    return Inter.getLocText("Height") + ": " + Utils.doubleToString(paramDouble1) + "/" + Utils.doubleToString(paramDouble2) + Inter.getLocText("px");
  }

  private String createToolTipString(int paramInt)
  {
    return paramInt + "R";
  }

  private void dealCellSelection(ReportPane paramReportPane)
  {
    GridSelection localGridSelection = paramReportPane.getGridSelection();
    int i = localGridSelection.getType();
    if (i == 1)
      return;
    TemplateReport localTemplateReport = paramReportPane.getEditingReport();
    Rectangle localRectangle = localGridSelection.getEditRectangle();
    int j = localRectangle.x;
    int k = localRectangle.y;
    int l = localRectangle.width;
    int i1 = localRectangle.height;
    for (CellElement localCellElement = localTemplateReport.getCellElement(j, k); localCellElement != null; localCellElement = localTemplateReport.getCellElement(j, k))
    {
      if ((localCellElement.getRowSpan() == 1) && (localCellElement.getColumnSpan() == 1))
        break;
      j += localCellElement.getColumnSpan();
    }
    localRectangle.setBounds(j, k, l, i1);
  }
}